import 'package:date_format/date_format.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/app_files.dart';

import 'action.dart';
import 'state.dart';

Effect<NbOrderViewState> buildEffect() {
  return combineEffects(<Object, Effect<NbOrderViewState>>{
    NbOrderViewAction.onLoad: _onLoad,
    NbOrderViewAction.onTapTime: _onTapTime,
    Lifecycle.initState: _initState,
  });
}

void _initState(Action action, Context<NbOrderViewState> ctx) async {
  ResultData res = await CommonNetWork.nbUserInfo(ctx.state.userID);
  if (!res.success) {
    await CommonUtils.showAlertView(ctx.context, res.desc);
    Navigator.pop(ctx.context);
  }
  Map list = res.data['list'];
  List items = list['Data'];
  if (items.isEmpty) {
    await CommonUtils.showAlertView(ctx.context, '未找到用户信息');
    Navigator.pop(ctx.context);
    return;
  }
  Map info = items.first;

  String lastRecordTime =
      CommonUtils.checkNullToString(info['UAmountLastChaobiaoDate']);
  DateTime dateTime = DateTime.tryParse(lastRecordTime);

  ctx.state.model = NbOrderModel(
    lateFee: CommonUtils.checkNullToString(info['UAmountLateFee']),
    balance: CommonUtils.checkNullToString(info['UAmountBalance']),
    lastPointNumber:
        CommonUtils.checkNullToString(info['UAmountChaobiaoNumber']),
    arrearsFee: CommonUtils.checkNullToString(info['UAmountArrearsFee']),
    gasMeterNo: CommonUtils.checkNullToString(info['GasMeterNo']),
    meterFactory: CommonUtils.checkNullToString(info['MeterFactory']),
    lastRecordTime:
        dateTime == null ? '-' : formatDate(dateTime, [yyyy, '-', mm, '-', dd]),
  );

  DateTime now = DateTime.now();
  ctx.state.startTime =
      ctx.state.endTime = formatDate(now, [yyyy, '-', mm, '-', dd]);
  _sendRequest(ctx, 1);
  ctx.dispatch(NbOrderViewActionCreator.onAction());
}

_sendRequest(Context<NbOrderViewState> ctx, int page) async {
  ResultData res = await CommonNetWork.nbUserBillList(
      userID: ctx.state.userID,
      startTime: ctx.state.startTime,
      endTime: ctx.state.endTime,
      page: page);
  if (res.success) {
    List data = res.data['Items'];
    List<NbOrderItemModel> items = data
        .map<NbOrderItemModel>((e) => NbOrderItemModel.fromJson(e))
        .toList();
    ctx.dispatch(
        NbOrderViewActionCreator.onGetData({'page': page, 'items': items}));
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
    List<NbOrderItemModel> items = [];
    ctx.dispatch(
        NbOrderViewActionCreator.onGetData({'page': page, 'items': items}));
  }
}

void _onLoad(Action action, Context<NbOrderViewState> ctx) async {
  await _sendRequest(ctx, action.payload);
}

void _onTapTime(Action action, Context<NbOrderViewState> ctx) async {
  DateTime now = DateTime.now();
  DateTime startTime = await CommonUtils.showMyDatePicker(
    ctx.context,
    desc: '请选择业务开始时间',
    initialDateTime: now,
    minimumDate: now.add(Duration(days: -60)),
    maximumDate: now,
  );
  if (startTime != null) {
    DateTime endTime = await CommonUtils.showMyDatePicker(
      ctx.context,
      desc: '请选择业务结束时间',
      initialDateTime: now,
      minimumDate: startTime,
      maximumDate: now,
    );
    print('-----------$startTime  $endTime');
    if (endTime != null) {
      ctx.state.startTime = formatDate(startTime, [yyyy, '-', mm, '-', dd]);
      ctx.state.endTime = formatDate(endTime, [yyyy, '-', mm, '-', dd]);
      ctx.state.items = null;
      ctx.dispatch(NbOrderViewActionCreator.onAction());
      await _sendRequest(ctx, 1);
    }
  }
}
